package com.self.campussecondhandmarket.controller.user;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.self.campussecondhandmarket.entity.domain.Goods;
import com.self.campussecondhandmarket.entity.request.goods.GoodsAll;
import com.self.campussecondhandmarket.entity.request.goods.NewGoods;
import com.self.campussecondhandmarket.entity.request.goods.*;
import com.self.campussecondhandmarket.entity.response.goods.*;
import com.self.campussecondhandmarket.resp.Result;
import com.self.campussecondhandmarket.service.GoodsService;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Author: Vincent
 * <p> DateTime: 2024/4/12 9:49
 * <p> Description: 商品视图
 * <p>
 **/
@Slf4j
@RestController
@RequestMapping("/api/goods")
@CrossOrigin(origins = "http://localhost:9999", allowCredentials = "true")
public class GoodsController {
	
	@Autowired
	GoodsService goodsService;
	
	/**
	 * 添加商品信息
	 */
	@PostMapping("/add")
	public Result<String> addGoodsInformation(@RequestBody @NonNull GoodsAddReq dto) {
		log.info("进入到goods --> add");
		return goodsService.addGoodsInformation(dto);
	}
	
	/**
	 * 查看同卖家的商品
	 */
	@PostMapping("/all")
	public Result<List<GoodsResp>> getAllGoods(@RequestBody @NonNull GoodsAll dto) {
		log.info("进入到goods --> all");
		List<GoodsResp> list = goodsService.getAllGoods(dto);
		return Result.success(list);
	}
	
	/**
	 * 移除商品
	 */
	@PostMapping("/remove")
	public Result<String> remove(@RequestBody @NonNull Action dto) {
		if (dto.getId() == null) {
			return Result.error("id不能为空");
		}
		log.info("进入到goods --> remove");
		goodsService.removeWithAction(dto);
		return Result.success("删除成功");
	}
	
	/**
	 * 查看商品详细数据
	 */
	@PostMapping("/detail")
	public Result<DetailResp> detail(@RequestBody @NonNull Detail dto) {
		log.info("进入到goods --> detail");
		if (dto.getGoodsId() == null) {
			return Result.error(null);
		}
		
		return Result.success(goodsService.getGoodsById(dto.getGoodsId()));
	}
	
	/**
	 * 查看更多商品
	 */
	@PostMapping("/more")
	public Result<List<More>> getGoodsById(@RequestBody @NonNull UserId id) {
		log.info("进入到goods --> userId");
		List<Goods> one = goodsService.list(Wrappers.<Goods>lambdaQuery().eq(Goods::getUserId, id.getUserId()));
		List<More> list = one.stream().map(o -> {
			More more = new More();
			BeanUtils.copyProperties(o, more);
			String img = o.getImg();
			List<String> strings = JSON.parseArray(img, String.class);
			more.setImg(strings);
			return more;
		}).toList();
		return Result.success(list);
	}
	
	/**
	 * 统计用户商品数量
	 */
	@PostMapping("/countByUserId")
	public Result<Long> count(@RequestBody @NonNull UserId id) {
		long count = goodsService.count(Wrappers.<Goods>lambdaQuery().eq(Goods::getUserId, id.getUserId()));
		return Result.success(count);
	}
	
	/**
	 * 获取学校分类
	 */
	@PostMapping("/school")
	public Result getSchool() {
		List<Map<String, String>> school = goodsService.list(Wrappers.<Goods>lambdaQuery()
				.select(Goods::getSchool)).stream().map(o -> {
			Map<String, String> vo = new HashMap();
			vo.put("school", o.getSchool());
			return vo;
		}).distinct().toList();
		return Result.success(school);
	}
	
	/**
	 * 查询校区全部商品信息
	 */
	@PostMapping("allBySchool")
	public Result<List<GoodsBySchool>> allBySchool(@RequestBody @NonNull Map<String, String> map) {
		List<GoodsBySchool> list = goodsService.list(Wrappers.<Goods>lambdaQuery().eq(Goods::getSchool, map.get("school"))).stream()
				.map(o -> {
					GoodsBySchool vo = new GoodsBySchool();
					BeanUtils.copyProperties(o, vo);
					List<String> strings = JSON.parseArray(o.getImg(), String.class);
					vo.setImg(strings);
					return vo;
				}).toList();
		return Result.success(list);
	}
	
	/**
	 * 推荐最新商品
	 */
	@PostMapping("/new")
	public Result newGoods(@RequestBody(required = false) NewGoods dto) {
		return goodsService.getNewGoods(dto);
	}
	
	
	/**
	 * 推荐位信息商品
	 */
	@PostMapping("/hot")
	public Result hot() {
		List<HotGoods> list = goodsService.list(Wrappers.<Goods>lambdaQuery()
				.eq(Goods::getTuijian, 1)
		).stream().map(o -> {
			HotGoods vo = new HotGoods();
			List<String> strings = JSON.parseArray(o.getImg(), String.class);
			vo.setId(o.getId());
			vo.setName(o.getName());
			vo.setPrice(o.getPrice());
			vo.setImg(strings);
			return vo;
		}).toList();
		return Result.success(list);
	}
	
	@PostMapping("/admin/all")
	public Result adminAll() {
		List<com.self.campussecondhandmarket.entity.response.goods.GoodsAll> vo = goodsService.list().stream().map(o -> {
			com.self.campussecondhandmarket.entity.response.goods.GoodsAll list = new com.self.campussecondhandmarket.entity.response.goods.GoodsAll();
			BeanUtils.copyProperties(o, list);
			list.setImg(JSON.parseArray(o.getImg(), String.class).get(0));
			list.setTuijian(o.getTuijian() == 1 ? Boolean.TRUE : Boolean.FALSE);
			return list;
		}).toList();
		return Result.success(vo);
	}
	
	@PostMapping("/del/{id}")
	public Result del(@PathVariable("id") Integer id) {
		goodsService.removeById(id);
		return Result.success("删除成功");
	}
	
	@PostMapping("/edit")
	public Result edit(@RequestBody @NonNull EditGoods goods) {
		Goods vo = new Goods();
		BeanUtils.copyProperties(goods, vo);
		goodsService.updateById(vo);
		return Result.success("修改成功");
	}
	
	/**
	 * 添加商品信息
	 */
	@PostMapping("/admin/add")
	public Result<String> addGoods(@RequestBody @NonNull AdminGoodsAdd dto) {
		log.info("进入到goods admin --> add");
		return goodsService.addGoodsInformation(dto);
	}
	
	@PostMapping("/admin/filter")
	public Result filter(@RequestBody @NonNull Filter dto) {
		List<Goods> list = goodsService.list(Wrappers.<Goods>lambdaQuery()
				.eq(dto.getSchool() != null, Goods::getSchool, dto.getSchool())
				.eq(dto.getType() != null, Goods::getTypes, dto.getType())
				.eq(dto.getTuijian() != null, Goods::getTuijian, dto.getTuijian())
		);
		List<com.self.campussecondhandmarket.entity.response.goods.GoodsAll> vo = list.stream().map(o -> {
			com.self.campussecondhandmarket.entity.response.goods.GoodsAll temp = new com.self.campussecondhandmarket.entity.response.goods.GoodsAll();
			BeanUtils.copyProperties(o, temp);
			temp.setImg(JSON.parseArray(o.getImg(), String.class).get(0));
			temp.setTuijian(o.getTuijian() == 1 ? Boolean.TRUE : Boolean.FALSE);
			return temp;
		}).toList();
		return Result.success(vo);
	}
}
